package com.geekhole.demo.v2.service.base;

import com.geekhole.demo.domain.Agg;
import com.geekhole.demo.domain.DomainServiceContext;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.repository.CrudRepository;

public abstract class AbstractCreateService<AGG extends Agg, CONTEXT extends DomainServiceContext>
        extends AbstractDomainService<AGG, CONTEXT>{

    public AbstractCreateService(CrudRepository<AGG, ?> repository, ApplicationEventPublisher eventPublisher) {
        super(repository, eventPublisher);
    }

    @Override
    protected AGG doHandle(CONTEXT context) {
        // 1. 生成 聚合根
        AGG agg = instance(context);

        // 2. 执行业务访问
        update(agg, context);

        // 3. 保存聚合根
        save2DB(agg);

        return agg;
    }


    protected void update(AGG agg, CONTEXT context){

    }

    protected abstract AGG instance(CONTEXT context);

}
